package chuishop.yiqimaicai.controller;

import chuishop.yiqimaicai.pojo.GoodsHouse;
import chuishop.yiqimaicai.pojo.UserInfo;
import chuishop.yiqimaicai.service.GoodsHouseService;
import chuishop.yiqimaicai.service.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @RequestMapping("findUserInfo")
    @ResponseBody
    public PageInfo<UserInfo> findUserInfo(Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);
        List<UserInfo> list=userInfoService.findUserInfo();
        PageInfo<UserInfo> pageInfo=new PageInfo<>(list);

        return pageInfo;
    }

    @RequestMapping("addUserInfo")
    @ResponseBody
    public Map<Object,Object> addUserInfo(@RequestBody UserInfo userInfo){

        Map<Object,Object> result=new HashMap<>();
        if(userInfoService.addUserInfo(userInfo)){
            result.put("status", "200");
            result.put("msg", "添加成功");
        } else {
            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

    @RequestMapping("delUserInfoByIds")
    @ResponseBody
    public Map<Object,Object> delUserInfoByIds(String ids){

        String strid[]=ids.split(",");
        Map<Object,Object> result=new HashMap<>();

        if(userInfoService.delUserInfoByIds(strid)){
            result.put("status","200");
            result.put("msg","删除成功");
        }else {
            result.put("status","400");
            result.put("msg","删除失败");
        }
        return result;
    }


    @RequestMapping("updataUserinfo")
    @ResponseBody
    public Map<Object,Object> updataUserinfo(@RequestBody UserInfo userInfo){
        Map<Object,Object> result=new HashMap<>();
        if (userInfoService.updataUserinfo(userInfo)){
            result.put("status","200");
            result.put("mag","添加成功");


        }else {
            result.put("status","500");
            result.put("msg","添加失败");
        }
        return result;
    }

    @RequestMapping("findUserInfoByname")
    @ResponseBody
    public List<UserInfo> findUserInfoByname(String usename){
        List<UserInfo> list=userInfoService.findUserInfoByname(usename);
        return list;
    }
}
